<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0 auto;
        }

        .box {
            position: absolute;
            width: 100px;
            height: 100px;
            background-color: purple;
            left: 0;
        }
        
        .box2 {
            position: absolute;
            width: 100px;
            height: 100px;
            background-color: yellow;
            left: 0;
            top: 120px;
        }

        .btn {
            position: absolute;
            top: 240px;
        }

        .btn2 {
            position: absolute;
            top: 280px;
        }
    </style>
</head>
<body>
    <div class="box">box1</div>
    <div class="box2">box2</div>
    <button class="btn">点击盒子匀速运动</button>
    <button class="btn2">点击盒子减速运动</button>
    <script>
        function moveTo(obj, target) {
            if (obj.flag) return;
            console.log(obj);
            obj.flag = true;
            obj.timer = setInterval(() => {
                if (obj.offsetLeft >= target) {
                    clearInterval(obj.timer);
                    obj.flag = false;
                }else {
                    obj.style.left = obj.offsetLeft + 1 + 'px';
                }
            }, 30);
        }
        function moveToDec(obj, target) {
            if (obj.flag) return;
            console.log(obj);
            obj.flag = true;
            obj.timer = setInterval(() => {
                if (obj.offsetLeft >= target) {
                    clearInterval(obj.timer);
                    obj.flag = false;
                }else {
                    obj.style.left = obj.offsetLeft + Math.ceil((target - obj.offsetLeft) / 10 ) + 'px';
                }
            }, 30);
        }
        var btn = document.querySelector('.btn');
        var btn2 = document.querySelector('.btn2');
        var box = document.querySelector('.box');
        var box2 = document.querySelector('.box2');
        btn.addEventListener('click', function() {
            moveTo(box, 400);
        });
        btn2.addEventListener('click', function() {
            moveToDec(box2, 400);
        })
    </script>
</body>
</html>